package com.dc.business.datacollectbusiness.controller;

import com.dc.business.datacollectbusiness.entity.Company;
import com.dc.business.datacollectbusiness.entity.Factories;
import com.dc.business.datacollectbusiness.model.Result;
import com.dc.business.datacollectbusiness.service.FactoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/factory")
@CrossOrigin
public class FactoryController {
    @Autowired
    private FactoriesService factoriesService;
    @GetMapping("/seletebyid")
    public Result getAllFactoriesByCompanyId(@RequestParam int companyId) {
        List<Factories> factories = factoriesService.getAllFactoriesByCompanyId(companyId);
        if (factories.isEmpty()) {
            return Result.error("factory not found");
        }else {
            return Result.success(factories);
        }
    }

    @DeleteMapping("/delete")
    public Result deleteFactories(@RequestParam int factoryId) {
        boolean b=false;
        b=factoriesService.deleteFactoriesById(factoryId);
        if(b!=false){
            return Result.success(b);
        }else {
            return Result.error("删除失败");
        }
    }

    @PostMapping("/insert")
    public Result insertCompany(@RequestBody Factories factories) {
        boolean b=false;
        b=factoriesService.insertFactories(factories);
        if(b!=false){
            return Result.success(b);
        }else {
            return Result.error("插入失败");
        }
    }

    @PostMapping("/update")
    public Result updateCompany(@RequestBody Factories factories) {
        boolean b=false;
        b=factoriesService.updateFactories(factories);
        if(b!=false){
            return Result.success(b);
        }else {
            return Result.error("修改失败");
        }
    }
}
